package com.starwish.starwishblog.web.back;

import com.github.pagehelper.PageInfo;
import com.starwish.starwishblog.entity.Tag;
import com.starwish.starwishblog.result.ResultResponse;
import com.starwish.starwishblog.service.TagService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;
@Slf4j
@RestController
@RequestMapping("admin/tag")
@Api(value = "tagcaozuo")
public class TagController {

    @Autowired
    TagService tagService;

    @Value("${starwish.user}")
    String path ;

    @GetMapping("/taglist")
    public ResultResponse<PageInfo<Tag>> tlist(@RequestParam(value = "now",defaultValue = "1") Integer now,
                                               @RequestParam(value = "size",defaultValue = "3")Integer size,
                                               String name){

        ResultResponse<PageInfo<Tag>> result = null;

        try {
            PageInfo<Tag> rolePageInfo = tagService.showTagByPage(now, size, name);
            result = new ResultResponse<>(200,"查询标签成功!",rolePageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询标签失败！");
        }
        return result;
    }



    @DeleteMapping("/delTag/{id}")
    public ResultResponse<Void> delTagById(@PathVariable("id") Integer id){
        ResultResponse<Void> result = null;

        try {
            tagService.removeBykey(id);
            result = new ResultResponse<>(200,"删除标签成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除标签失败!");
        }
        return result ;
    }

    @PostMapping("/addTag")
    @ApiOperation(value = "tinjia")
    public ResultResponse<Void> add(@RequestBody Tag tag){
        ResultResponse<Void> result = null;

        try {
            tag.setCreateTime(new Date());
            tag.setModifyTime(new Date());
            tagService.addTag(tag);
            result = new ResultResponse<>(200,"添加标签成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加标签失败!");
        }
        return result ;
    }

    @PostMapping("/modify")
    public ResultResponse<Void> modify(@RequestBody Tag tag){
        ResultResponse<Void> result= null;

        try {
            tagService.modifytag(tag);
            tag.setModifyTime(new Date());
            result=new ResultResponse<>(200,"修gai成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改失败");
        }
        return  result;
    }
    @RequestMapping("upload")
    public ResultResponse<String> upload   (@RequestParam(value = "avatar") MultipartFile multipartFile){
        ResultResponse<String> result = null;
        try {
            //获取上传的名字
            String filename = multipartFile.getOriginalFilename();
            //上传文件的原名字
            log.info(filename);
            //修改上传文件的名字
            filename= UUID.randomUUID().toString().replaceAll("-", "")+filename.substring(filename.lastIndexOf("."));
            //上传修改后的名字
            log.info("上传修改后的名字"+filename);
            File file = new File(path);
            if (!file.exists()){
                file.mkdirs();
            }

            //创建上传文件得对象
            File f = new File(file,filename);
            multipartFile.transferTo(f);
            result = new ResultResponse<>(200,"上传成功",filename);
        } catch (IOException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"上传失败");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(202,"系统错误");
        }
        return result;
    }




}
